<?php

/*
Copyright 2007 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


require_once('BH_Widget_Data.php');
require_once('BH_Widget.php');

abstract class BH_Widget_Base implements BH_Widget {

	protected $widget;
	
	function __construct($widget) {
		$this->widget = $widget;
	}
	
	/* BH_Widget API */
	
	function widget($arg) {
	}
	
	
	/* convenience functions */
	
	function get_name() {
		return $this->widget->widget_name;
	}
	
	function set_name($name) {
		global $bhwd;
		$widget->widget_name = $name;
		$bhwd->set_widget_name($this->widget->widget_id, $this->widget->widget_name);
	}
	
	function get_attributes() {
		return $this->widget->widget_attributes ? $this->widget->widget_attributes : array();
	}
	
	function set_attributes($attributes) {
		global $bhwd;
		$this->widget->widget_attributes = $attributes ? $attributes : array();
		$bhwd->set_widget_attributes($this->widget->widget_id, $this->widget->widget_attributes);
	}
	
	function get_attribute($k) {
		return $this->widget->widget_attributes[$k];
	}
	
	function set_attribute($k, $v) {
		global $bhwd;
		$this->widget->widget_attributes[$k]=$v;
		$this->set_attributes($this->widget->widget_attributes);
	}
	
	function get_widget_start_tag($args) {
		extract($args);
		
		if (!$widget_tag) 
			return '';
			
		$x  = '<'. $widget_tag;
		$x .= ' class="' .  ($widget_class ? "$widget_class " : '') . get_class($this) .'"';
		$x .= ' id="'. $this->get_name() .'"';
		
		if ($style = $this->get_attribute('style'))
			$x .= ' style="' . $style .'"';
		
		$x .= '>';
		
		return $x;
	}
	
	function get_widget_end_tag($args) {
		extract($args);
		
		if (!widget_tag)
			return;
			
		return '</'. $widget_tag .'>';
	}
	
	function nice_name() {
		return preg_replace("/\W+/", "-", $this->get_name());
	}
	
}

abstract class BH_Controllable_Widget_Base extends BH_Widget_Base implements BH_Controllable_Widget {

	function control() {
	}
	
	function get_request_param_name($param_name) {
		return $param_name .'-'. $this->nice_name();
	}
	
	function get_request_param_value($param_name, $striptag=true) {
	
		$value = $_POST[$this->get_request_param_name($param_name)];
	
		if (is_string($value)) {
			$value = stripslashes($_POST[$this->get_request_param_name($param_name)]);
			
			if ($striptag)
				return strip_tags($value);
		}
		return $value;
	}
	
	function update_attribute_from_param($param, $striptag=true) {
		$this->set_attribute($param, $this->get_request_param_value($param, $striptag));
	}
}
	
?>
